package com.eunion.web.cms.scheduled;

import java.util.List;

import org.apache.commons.lang.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Configuration;
import org.springframework.scheduling.annotation.EnableScheduling;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;

import com.eunion.common.util.BoShiGxmd;
import com.eunion.common.util.CommUtils;
import com.eunion.web.admin.entity.TableColumnDic;
import com.eunion.web.admin.entity.spec.TableColumnDicSpec;
import com.eunion.web.admin.repository.TableColumnDicRepository;
import com.eunion.web.cms.entity.University;
import com.eunion.web.cms.repository.UniversityRepository;

@Component
@Configuration      //1.主要用于标记配置类，兼备Component的效果。
@EnableScheduling
public class UninversityScheduled {

    private static Logger LOGGER = LoggerFactory.getLogger(BoShiGxmd.class);
    @Autowired
    private UniversityRepository repository;

    @Autowired
    private TableColumnDicRepository dicRepository;
    
    //@Scheduled(initialDelay = 10000 , fixedDelay = 1000*10)
    public void intelligenceTags() {
        List<TableColumnDic> configs = dicRepository.findAll(TableColumnDicSpec.build("config_university_tags"));
        if (CommUtils.isEmpty(configs)) {
            return;
        }
        System.out.println("intelligenceTags...");
        String configValue = configs.get(0).getColumnValue();
        if ("1".equals(configValue)) {
            List<University> list = repository.findAll();
            for(University uninversity : list) {
                String tags = uninversity.getTags();
                if (StringUtils.isBlank(tags) || tags.contains("#")) {
                    continue;
                }
                String[] arrayTags = tags.split(" ");
                if (arrayTags.length == 0) {
                    continue;
                }
                StringBuffer retSb = new StringBuffer();
                retSb.append("#");
                for (String tag : arrayTags) {
                    if ("收起".equals(tag) || "展开".equals(tag)){
                        continue;
                    }
                    if(tag.contains("（")){
                        String toRetTag = tag.substring(0,tag.indexOf("（"));
                        if ("".equals(toRetTag)){
                            continue;
                        }
                        if (retSb.toString().contains(toRetTag+"#")){
                            continue;
                        }
                        retSb.append(toRetTag+"#");
                    } else {
                        if (retSb.toString().contains(tag+"#")){
                            continue;
                        }
                        retSb.append(tag+"#");
                    }
                }
                
                uninversity.setTags(retSb.toString());
                repository.saveAndFlush(uninversity);
            }
        }
    }
}
